/*
搜索页面的js
 */

var allSorts;
// 搜索商品
function searchGoods(name,firstSortId){
    $.ajax({
        url:'/api/book/search',
        type:'get',
        data:{name:name,firstSortId:firstSortId},
        dataType:'json',
        success:function (data) {
            var html = "";
            $.each(data,function (index,item) {
                if (data === null){

                }else {
                    if ((index + 1) % 4 == 0){
                        html += '<li class="r">'
                    }else{
                        html += '<li>'
                    }
                    if (item.fileUrl === null){
                        html +=
                            '<a href="book?id='+item.id+'"> \
                            <div class="img">\
                            <img src="/imgs/default.jpg">\
                            </div>\
                            <h2 class="desc">'+item.name+'</h2>\
                            <p class="price">'+item.price+'元 起</p>\
                            <div class="thumbs">\
                                <ul class="clear">';
                    }else {
                        html +=
                           '<a href="book?id='+item.id+'"> \
                            <div class="img">\
                            <img src="/upload/'+item.fileUrl[0]+'">\
                            </div>\
                            <h2 class="desc">'+item.name+'</h2>\
                            <p class="price">'+item.price+'元 起</p>\
                            <div class="thumbs">\
                                <ul class="clear">';
                            $.each(item.fileUrl,function (i,pic) {
                                html +=   '<li><img src="/upload/'+pic+'" ></li>';
                            });
                    }
                    html += '</ul>\
                        </div>\
                        <div class="flags">\
                        <span>赠</span>\
                        <span>加价购</span>\
                       </div>\
                        </a>\
                        </li>';
                }
            });

            $(".goods>ul").html(html);

        },
        error:function () {

        }
    });
}

/**
 * 获取一级分类
 */
function getFirstSort() {
    $.ajax({
        url: '/api/bookSort/getSort',
        type:'get',
        date:{},
        dataType: 'json',
        success:function (data) {

            // 保存所有的分类信息

            allSorts = data;
            var html = '<span class="label">一级分类:</span>'

            if (bookSortId == -1){
                html +='<li class="active"><a href="javascript:;" data-sort="-1">全部</a></li>';
            }else{
                html +='<li ><a href="javascript:;" data-sort="-1">全部</a></li>';
            }

            /*$.each(data,function (index,item) {
                if (item.id == getFirstSortId(data,bookSortId)){
                    html += '<li class="active"><a href="javascript:;" data-sort="'+item.id+'">'+item.name+'</a></li>';
                }else{
                    html += '<li><a href="javascript:;" data-sort="'+item.id+'">'+item.name+'</a></li>';
                }
            });*/
            $.each(data,function (index,item) {
              if (item.id == bookSortId){
                  html += '<li class="active"><a href="javascript:;" data-sort="'+item.id+'">'+item.name+'</a></li>';
              }else{
                  html += '<li><a href="javascript:;" data-sort="'+item.id+'">'+item.name+'</a></li>';
              }
            });
            $(".first-sort").html(html);

            //生成二级分类
                // 调用方法 来生成二级分类
            initSecondSort(data,0);
            // $(".second-sort").hide();

        },
        error:function () {

        }
    });
}

function initSecondSort(data ,secendSortId){
    //遍历查找当前的一级分类
    for (var i = 0; i < data.length ; i++){

            var html = '<span class="label">二级分类:</span>'

            if (secendSortId == 0){
                html +='<li class="active"><a href="javascript:;" data-sort="-1">全部</a></li>';
            }else{
                html +='<li ><a href="javascript:;" data-sort="-1">全部</a></li>';
            }
            //遍历data[i]里面的二级分类sonSorts 渲染到页面
            $.each(data[i].children,function (index,item) {
                if (item.id == secendSortId){
                    html += '<li class="active"><a href="javascript:;" data-sort="'+item.id+'">'+item.name+'</a></li>';
                }else{
                    html += '<li><a href="javascript:;" data-sort="'+item.id+'">'+item.name+'</a></li>';
                }
            });
            $(".second-sort").html(html);
            $(".second-sort").show();
            break;
    }
}

function getFirstSortId(data,secondSortId) {
    // 遍历一级分类
    for (var i = 0; i < data.length; i++){
        if (data[i].children == null) continue;
        // 遍历二级分类
        for (var j = 0; j < data[i].children.length; j++){

            if (data[i].children[j].id == secondSortId){
                return data[i].id;
            }
        }
    }
    return 0;
}

/**
 * 给一级分类绑定的事件,预绑定
 */
$(".first-sort").on("click","a",function () {
    $(".first-sort li").removeClass("active");
    $(this).parent().addClass("active");
    var firstSrotId = $(this).attr("data-sort");
    bookSortId = 0;

    //切换二级分类
    if (firstSrotId != -1){
        initSecondSort2(allSorts,firstSrotId);
    }else{
        $(".second-sort").hide();
    }
    // 重新刷新数据
    searchGoods(name,firstSrotId);

});


$(".second-sort").on("click","a",function () {
    $(".second-sort li").removeClass("active");
    $(this).parent().addClass("active");

    var secendSort = $(this).attr("data-sort");
    var firstSortId = $(".first-sort .active a").attr("data-sort");

    console.log(firstSortId)
    console.log(secendSort)
    // 重新刷新数据
    // searchGoods(name,bookSortId,firstSortId);
    searchGoodsBySecendSort(secendSort,firstSortId)
});

//创建二级菜单
function initSecondSort2(data ,firstSrotId){
    console.log(data)
    //遍历查找当前的一级分类
    for (var i = 0; i < data.length ; i++){

        var html = '<span class="label">二级分类:</span>'

        if (firstSrotId == 0){
            html +='<li class="active"><a href="javascript:;" data-sort="0">全部</a></li>';
        }else{
            html +='<li ><a href="javascript:;" data-sort="-1">全部</a></li>';
        }
        //遍历data[i]里面的二级分类sonSorts 渲染到页面
        if (data[i].id == firstSrotId){
            $.each(data[i].children,function (index,item) {
                if (item.id == firstSrotId){
                    html += '<li class="active"><a href="javascript:;" data-sort="'+item.id+'">'+item.name+'</a></li>';
                }else{
                    html += '<li><a href="javascript:;" data-sort="'+item.id+'">'+item.name+'</a></li>';
                }
            });
            $(".second-sort").html(html);
            $(".second-sort").show();
            break;
        }


    }
}

function searchGoodsBySecendSort(secendSort,firstSortId){
    $.ajax({
        url: '/api/book/getBySort',
        type:'get',
        data:{firstSortId:firstSortId,sortId:secendSort},
        dataType: 'json',
        success:function (data) {
            var html = "";
            $.each(data,function (index,item) {
                if ((index + 1) % 4 == 0){
                    html += '<li class="r">'
                }else{
                    html += '<li>'
                }
                html +=
                    '<a href="book?id='+item.id+'"> \
                    <div class="img">\
                    <img src="/upload/'+item.fileUrl[0]+'">\
                    </div>\
                    <h2 class="desc">'+item.name+'</h2>\
                    <p class="price">'+item.price+'元 起</p>\
                    <div class="thumbs">\
                        <ul class="clear">';
                $.each(item.fileUrl,function (i,pic) {
                    html +=   '<li><img src="/upload/'+pic+'" ></li>';
                });


                html += '</ul>\
                    </div>\
                    <div class="flags">\
                    <span>赠</span>\
                    <span>加价购</span>\
                   </div>\
                    </a>\
                    </li>';
            });
            $(".goods>ul").html(html);
        }
    })
}